<?php

namespace Admin\Controller;

/**
 * 权限管理
 * Class PrivilegeController
 * @package Admin\Controller
 */
class PrivilegeController extends CommonController
{
    /**
     * 添加权限操作
     */
    public function addPrivilege()
    {
        if (IS_POST) {
            $privilegeModel = D('Admin/Privilege')->where('show_on_menubar=0');
            if ($privilegeModel->create(I('post.'), 1)) {
                if ($id = $privilegeModel->add()) {
                    $this->success('权限添加成功!');
                }
            }
            // $this->error($privilegeModel->getError());
        }else{
            $privilegeModel = D('Admin/Privilege')->where('show_on_menubar=0');
            $parentData = $privilegeModel->getTree();
            $this->assign('parentData', $parentData);
            $this->display('add_privilege'); 
        }
        
    }

    /**
     * 编辑权限操作
     */
    public function editPrivilege()
    {
        $id = I('get.id', 0, 'intval');
        if (IS_POST) {
            $model = D('Admin/Privilege');
            if ($model->create(I('post.'), 2)) {
                if ($model->save() !== FALSE) {
                    $this->success('权限修改成功！');
                    //exit;
                }
            }
            //$this->error($model->getError());
        }else{
            $model = M('Privilege')->where('show_on_menubar=0');
            $data = $model->find($id);
            $parentModel = D('Admin/Privilege')->where('show_on_menubar=0');
            $parentData = $parentModel->getTree();
            $children = $parentModel->getChildren($id);
            $this->assign('data', $data);
            $this->assign(array(
                'parentData' => $parentData,
                'children' => $children,
            ));
            $this->display();
        }
        
    }

    /**
     * 删除权限操作
     */
    public function ajaxDeletePrivilege()
    {
        
        if (M('Privilege')->delete(I('get.id', 0)) !== false) {
            $this->success('权限删除成功!', '', true);
        } else {
            $this->error(M('Privilege')->getError(), '', true);
        }
    }

    /**
     * 权限列表显示
     */
    public function listPrivilege()
    {
        $privilegeModel = D('Admin/Privilege');
        $count = M('Privilege')->where('show_on_menubar=0')->count();
        //$count = count($privilegeDatas);
        $page = getpage($count,20);
        $show = $page->show();
        $privilegeData = $privilegeModel->limit($page->firstRow.','.$page->listRows)->getTree();
        
        $this->assign('privilegeData', $privilegeData);
        $this->assign('count', $count);
        $this->assign('page',$show);
        $this->display('list_privilege');
    }
}